<template>
  <a-modal
    :width='1400'
    :visible='visible'
    :confirm-loading='loading'
    :title="isUpdate ? '修改' : '新建'"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible='updateVisible'
    @ok='save'
  >
  <div class='ele-body ele-body-card'>
    <a-card
      title="参数设置"
      :bordered='true'
      :body-style="{padding: '20px'}">
      <a-form
        ref='formRef'
        :model='form'
        :rules="rules"
        :label-col='{ md: { span: 16 }, sm: { span: 24 } }'
        :wrapper-col='{ md: { span: 8 }, sm: { span: 24 } }'
      >
        <a-row :gutter="8">
          <a-col :md="12" :sm="24" :xs="24">
            <a-form-item label='模板名称:' name='templateName'>
              <a-input v-model:value='form.templateName' allow-clear style="width: 300px;" />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24" :xs="24" />
        </a-row>
        <a-divider orientation="left">饮水台喷淋参数</a-divider>
        <a-alert message="注意：如果第一路(温度)上下限没开启，喷淋开启是不管用" type="warning" />
        <a-row :gutter='8'>
          <a-col :md='12' :sm='24' :xs='24'>
            <a-form-item label="温度上限报警开启:" name="highAlarmSwitch">
              <a-select v-model:value="form.highAlarmSwitch">
                <a-select-option :value="0">两路同时关闭</a-select-option>
                <a-select-option :value="1">第一路(温度)上限开启</a-select-option>
                <a-select-option :value="2">第二路(湿度)上限开启</a-select-option>
                <a-select-option :value="3">两路同时开启</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24' :xs='24' />
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='12' :sm='24' :xs='24'>
            <a-form-item label="温度下限报警开启:" name="lowAlarmSwitch">
              <a-select v-model:value="form.lowAlarmSwitch">
                <a-select-option :value="0">两路同时关闭</a-select-option>
                <a-select-option :value="1">第一路(温度)上限开启</a-select-option>
                <a-select-option :value="2">第二路(湿度)上限开启</a-select-option>
                <a-select-option :value="3">两路同时开启</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24' :xs='24' />
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='12' :sm='24' :xs='24'>
            <a-form-item label='报警温度上限(整数):' name='tempAlarmHigh'>
              <a-input v-model:value='form.tempAlarmHigh' placeholder='报警温度上限' allow-clear />
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24' :xs='24' />
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='12' :sm='24' :xs='24'>
            <a-form-item label='报警温度下限(整数):' name='tempAlarmLow'>
              <a-input v-model:value='form.tempAlarmLow' placeholder='报警温度下限' allow-clear />
            </a-form-item>
          </a-col>
          <a-col :md='14' :sm='24' :xs='24' />
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='12' :sm='24' :xs='24'>
            <a-form-item label='合格温度(报警湿度)上限(整数):' name='standardTempAlarmHigh'>
              <a-input v-model:value='form.standardTempAlarmHigh' placeholder='请输入合格温度(报警湿度)上限，单位℃' allow-clear />
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24' :xs='24' />
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='12' :sm='24' :xs='24'>
            <a-form-item label='合格温度(报警湿度)下限(整数):' name='standardTempAlarmLow'>
              <a-input v-model:value='form.standardTempAlarmLow' placeholder='请输入合格温度(报警湿度)下限，单位℃' allow-clear />
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24' :xs='24' />
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='12' :sm='24' :xs='24'>
            <a-form-item label='采集间隔时间(分钟,范围1-2880):' name='collectTime'>
              <a-input v-model:value='form.collectTime' placeholder='采集间隔时间，单位分钟' allow-clear />
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24' :xs='24' />
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='12' :sm='24' :xs='24'>
            <a-form-item label='发送间隔时间(分钟,范围1-2880):' name='sendTime'>
              <a-input v-model:value='form.sendTime' placeholder='发送间隔时间，分钟' allow-clear />
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24' :xs='24' />
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='12' :sm='24' :xs='24'>
            <a-form-item label="喷淋使能:" name="sprayWork">
              <a-select v-model:value="form.sprayWork">
                <a-select-option :value="0">无效</a-select-option>
                <a-select-option :value="1">有效</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24' :xs='24' />
        </a-row>
      </a-form>
    </a-card>
  </div>
  </a-modal>
</template>

<script>
import {
  DeviceDrinkingStationConfigTemplateApi
} from "@/api/property/drinkingstation/DeviceDrinkingStationConfigTemplateApi";
import { message } from 'ant-design-vue';

export default {
  name: 'DeviceDrinkstationDeviceTemplateDrinkingstationConfigTemplateEdit',
  components: {
  },
  emits: ['done', 'update:visible'],
  props: {
    // 弹窗是否打开
    visible: Boolean,
    data: Object
  },
  created() {
    if (this.data) {
      this.form = Object.assign({}, this.data);
      this.isUpdate = true;
    } else {
      this.form = {};
      this.isUpdate = false;
    }
  },
  data() {
    return {
      loading : false,
      form : {},
      rules : {},
      // 是否是修改
      isUpdate: false,
      showSelectDevice : false,
      current : null

    };
  },
  methods: {
    updateVisible(value) {
      this.$emit('update:visible', value);
    },

    save() {
      if (this.form.tempAlarmLow < -50 || this.form.tempAlarmLow > 100) {
        message.error('温度上限应该在-50-100℃');
        return;
      }
      if (this.form.tempAlarmHigh < -50 || this.form.tempAlarmHigh > 100) {
        message.error('温度下限应该在-50-100℃');
        return;
      }

      if (this.form.collectTime < 1 || this.form.collectTime > 2880) {
        message.error('采集时间范围1-2880分钟');
        return;
      }
      if (this.form.sendTime < 1 || this.form.sendTime > 2880) {
        message.error('发送时间范围1-2880分钟');
        return;
      }

      this.loading = true;
      let result = null;
      // 执行编辑或修改
      if (this.isUpdate) {
        result = DeviceDrinkingStationConfigTemplateApi.edit(this.form);
      } else {
        result = DeviceDrinkingStationConfigTemplateApi.add(this.form);
      }
      result
        .then(result => {
          this.loading = false;
          message.success(result.message);

          if (!this.isUpdate) {
            this.form = {};
          }
          // 关闭弹框，通过控制visible的值，传递给父组件
          this.updateVisible(false);
          // 触发父组件done事件
          this.$emit('done');
        })
        .catch(() => {
          this.loading = false;
        });
    }
  }
};
</script>
<style scoped>
/** 用户卡片 */
.workplace-user-card .ele-cell-content {
  overflow: hidden;
}

.workplace-user-card h4 {
  margin-bottom: 6px;
}
</style>
